package com.itlong.cloud.controller.project;

import com.itlong.cloud.POJO.DTO.operate.OperateDeviceQRCodePageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceQRCodeInfoAllVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.equip.operate.IOperateDeviceQRCodeServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-设备二维码管理控制器
 * </desc>
 *
 * @createDate2020/2/24
 **/
@RestController
@RequestMapping("/project/qrCode")
public class OperateDeviceQRCodeController extends BaseController {

    @Autowired
    private IOperateDeviceQRCodeServiceClient iOperateDeviceQRCodeServiceClient;

    /**
     * <desc>
     *      分页获取设备二维码数据
     * </desc>
     *
     * @param operateDeviceQRCodePageDTO 查询参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/2/25
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(value = "/getDeviceQRCodeByPage",method = RequestMethod.POST)
    public Object getDeviceQRCodeByPage(OperateDeviceQRCodePageDTO operateDeviceQRCodePageDTO,HttpServletRequest request){
        try {
            operateDeviceQRCodePageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceQRCodePageDTO.class,request);
            if (operateDeviceQRCodePageDTO.getUseStatus() != null && !NumberHandlerUtil.isInRange(operateDeviceQRCodePageDTO.getUseStatus(),1,3)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            OperateDeviceQRCodeInfoAllVO operateDeviceQRCodeInfoAllVO = iOperateDeviceQRCodeServiceClient.getDeviceQRCodeByPage(operateDeviceQRCodePageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operateDeviceQRCodeInfoAllVO);
        }catch (Exception e){
            throw new DataAccessException("【运营-项目管理-分页获取设备二维码数据getDeviceQRCodeByPage】分页获取设备二维码数据失败",e);
        }
    }

    /**
     * <desc>
     *      批量添加设备二维码
     * </desc>
     *
     * @param operateDeviceQRCodePageDTO 二维码创建数量
     * @return
     * @Author Zhu.yj
     * @createDate  2020/2/25
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_QRCODE_MANAGE_ADD_LOG_DESC)
    @RequestMapping(value = "/batchSave",method = RequestMethod.POST)
    public Object batchSave(OperateDeviceQRCodePageDTO operateDeviceQRCodePageDTO,HttpServletRequest request){
        try {
            operateDeviceQRCodePageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceQRCodePageDTO.class,request);
            if (!NumberHandlerUtil.isInRange(operateDeviceQRCodePageDTO.getAmount(),1,99)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            Integer count = iOperateDeviceQRCodeServiceClient.batchSave(operateDeviceQRCodePageDTO);
            if (count == -1){
                return new MessageVO(OperateErrorCodeEnum.DEVICE_QR_CODE_MAX_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-批量创建设备二维码batchSave】批量创建二维码失败",e);
        }
    }

    /**
     * <desc>
     *      批量更改设备二维码状态
     * </desc>
     *
     * @param operateDeviceQRCodePageDTO 更新参数DTO
     * @return 受影响行数
     * @Author Zhu.yj
     * @createDate  2020/2/25
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_QRCODE_MANAGE_UPDATE_LOG_DESC)
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    public Object updateStatus(OperateDeviceQRCodePageDTO operateDeviceQRCodePageDTO,HttpServletRequest request){
        try{
            operateDeviceQRCodePageDTO = HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceQRCodePageDTO.class,request);
            if (operateDeviceQRCodePageDTO.getOperType() == null || StringUtils.isBlank(operateDeviceQRCodePageDTO.getCodes())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(operateDeviceQRCodePageDTO.getOperType(),0,2)){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            if (operateDeviceQRCodePageDTO.getOperType() == 0){
                List<Map<String, Object>> codeStatusList = iOperateDeviceQRCodeServiceClient.getQRCodeStatus(operateDeviceQRCodePageDTO.getCodes());
                for (Map<String, Object> codeStatusMap : codeStatusList){
                    if (codeStatusMap.get("useStatus").toString().equals("2") || codeStatusMap.get("status").toString().equals("2")){
                        return new MessageVO(OperateErrorCodeEnum.DEVICE_QR_CODE_COULD_NOT_DELETE_ERR.getErrorCode());
                    }
                }
            }
            iOperateDeviceQRCodeServiceClient.updateStatus(operateDeviceQRCodePageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营-更改设备二维码状态updateStatus】更改二维码状态失败",e);
        }
    }
}
